package com.zip.player;

import android.content.Context;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

import com.zip.z4player.R;


public class PlayerFragment extends Fragment {
    final String Tag = "PlayerFragment_Tag";

    VideoView mVideoView;
    TextView show;
    MediaController mediaController;

    //播放路径
    String path;
    Uri uri;

    //调节音量
    private AudioManager mAudioManager;

    //滑动的距离
    int ViewHeight;
    int ViewWidth;
    private float mMoveLenX = 0;
    private float mMoveLenY = 0;
    private float mStartDownX;
    private float mStartDownY;
    private float mLastDownX;
    private float mLastDownY;

    public PlayerFragment() {

    }

    public PlayerFragment(String str) {
        this.path = str;
    }

    public PlayerFragment(Uri uri) {
        this.uri = uri;
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    show.setVisibility(View.GONE);
                    break;

            }
        }
    };

    void show(String str) {
        handler.removeMessages(1);
        show.setText(str);
        show.setVisibility(View.VISIBLE);
        handler.sendEmptyMessageDelayed(1, 1200);
    }


    public void VideoPlayPath(String str) {
        Log.d(Tag, str);
        mVideoView.setVideoPath(str);
        mVideoView.start();
    }

    public void VideoPlayURI(Uri uri) {
        mVideoView.setVideoURI(uri);
        mVideoView.start();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_player, container, false);
        mAudioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);

        show = (TextView) view.findViewById(R.id.textview);
        mVideoView = (VideoView) view.findViewById(R.id.videoView);
        mediaController = new MediaController(getContext());

        mVideoView.setMediaController(mediaController);
        if (path != null && path.length() > 2) { //path=Environment.getExternalStorageDirectory().getPath() + "/手机操作录像.mp4";
            mVideoView.setVideoPath(path);
            mVideoView.start();
        } else if (uri != null) {
            mVideoView.setVideoURI(uri);
            mVideoView.start();
        }

        mVideoView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getActionMasked()) {
                    case MotionEvent.ACTION_DOWN:
                        doDown(motionEvent);
                        break;
                    case MotionEvent.ACTION_MOVE:
                        doMove(motionEvent);
                        break;
                    case MotionEvent.ACTION_UP:
                        return doUp(motionEvent);
                    // break;
                }
                return true;
            }
        });

        return view;
    }


    //region 触摸事件
    //0:单击  1:音量/亮度    2:播放位置
    int TouchFlag = 0;

    private void doDown(MotionEvent event) {
        TouchFlag = 0;
        ViewHeight = mVideoView.getHeight();
        ViewWidth = mVideoView.getWidth();
//        Log.d(Tag, "DownLocation:" + (int) event.getX() + "," + (int) event.getY());

        mMoveLenX = 0;
        mMoveLenY = 0;
        mStartDownX = event.getX();
        mStartDownY = event.getY();
        mLastDownX = event.getX();
        mLastDownY = event.getY();
    }

    private void doMove(MotionEvent event) {
//        Log.d(Tag, "MoveLocation:" + (int) event.getX() + "," + (int) event.getY());
        mMoveLenX += (event.getX() - mLastDownX);
        mMoveLenY += (event.getY() - mLastDownY);
        mLastDownX = event.getX();
        mLastDownY = event.getY();

        switch (TouchFlag) {
            case 0://0:单击
                if (Math.abs(mMoveLenX) > 10) {
                    TouchFlag = 2;
                } else if (Math.abs(mMoveLenY) > 10) {
                    TouchFlag = 1;
                }
                break;
            case 1: //1:音量/亮度
                if (mStartDownX > ViewWidth / 2) {
                    if (mMoveLenY > 30) {
                        mMoveLenY -= 30;
                        mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, //音量类型
                                AudioManager.ADJUST_LOWER,
                                0);
                        show("音量:" + mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
                    } else if (mMoveLenY < -30) {
                        mMoveLenY += 30;
                        mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, //音量类型
                                AudioManager.ADJUST_RAISE,
                                0);
                        show("音量:" + mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
                    }
                } else if (mStartDownX < ViewWidth / 2) {
                    setBrightness(0 - (int) mMoveLenY / 2);
                    mMoveLenY = 0;
                }
                break;
            case 2: //2:播放位置
                if (mMoveLenX > 30) {
                    mMoveLenX -= 30;
                    mVideoView.seekTo(mVideoView.getCurrentPosition() + 1000);
                    show(getTimeFromMillisecond(mVideoView.getCurrentPosition()));
                } else if (mMoveLenX < -30) {
                    mMoveLenX += 30;
                    mVideoView.seekTo(mVideoView.getCurrentPosition() - 1000);
                    show(getTimeFromMillisecond(mVideoView.getCurrentPosition()));

                }
                break;
        }

    }

    private boolean doUp(MotionEvent event) {
//        Log.d(Tag, "UpLocation:" + (int) event.getX() + "," + (int) event.getY());

        if (TouchFlag > 0) return true;
        else return false;
    }
    //endregion


    //region 设置屏幕亮度 0 最暗 255 最亮
    public void setBrightness(float brightness) {
        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
        lp.screenBrightness = lp.screenBrightness + brightness / 255.0f;
        if (lp.screenBrightness > 1) {
            lp.screenBrightness = 1;
        } else if (lp.screenBrightness < 0.01) {
            lp.screenBrightness = (float) 0.01;
        }
        getActivity().getWindow().setAttributes(lp);

        float sb = lp.screenBrightness;
        String str = (int) Math.ceil(sb * 100) + "%";
        show("亮度:" + str);
//        String str = "调节亮度";
//        if (mToast == null) mToast = Toast.makeText(getContext(), str, Toast.LENGTH_SHORT);
//        else mToast.setText(str);
//        mToast.show();

        //brightnessTextView.setText((int) Math.ceil(sb * 100) + "%");
    }
    //endregion

    //region 从时间(毫秒)中提取出时间(时:分)
    public String getTimeFromMillisecond(int millisecond) {
        String time = "";
        int hour = millisecond / 1000 / 60 / 60;
        long minute = millisecond / 1000 / 60 % 60;
        long second = millisecond / 1000 % 60;
        if (hour > 0)
            time = hour + ":";
        if (minute < 10) {
            time += "0";
        }
        time += minute + ":";
        if (second < 10) {
            time += "0";
        }
        time += second;
        return time;

    }
    //endregion
}
